package com.mall.shopping.services;

import com.mall.shopping.IContentService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-22 9:59
 */
@Slf4j
@Service
public class IContentServiceImpl implements IContentService {
    @Autowired
    private PanelContentMapper panelContentMapper;
    @Autowired
    private ContentConverter contentConverter;

    @Override
    public NavListResponse queryNavList() {
        NavListResponse navListResponse = new NavListResponse();

        try {
            Example examplePanelContent = new Example(PanelContent.class);
            Example.Criteria panelContentCriteria = examplePanelContent.createCriteria();
            panelContentCriteria.andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);


            List<PanelContent> panelContents = panelContentMapper.selectByExample(examplePanelContent);
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);

            navListResponse.setPannelContentDtos(panelContentDtos);
            navListResponse.setCode(ShoppingRetCode.SHOPPING_HOMEPAGE.getCode());
            navListResponse.setMsg(ShoppingRetCode.SHOPPING_HOMEPAGE.getMessage());

            return navListResponse;
        } catch (Exception e) {
            navListResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            navListResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
        }


        return navListResponse;
    }
}
